home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSDEVICE.C < prev    next >
C/C++ Source or Header  |  1993-05-26  |  20KB  |  655 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsdevice.c */
  20. /* Device operators for Ghostscript library */
  21. #include "math_.h"            /* for fabs */
  22. #include "memory_.h"            /* for memcpy */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsprops.h"
  26. #include "gsutil.h"
  27. #include "gxarith.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"
  30. #include "gxdevmem.h"
  31.  
  32. /* Import the device list from gdevs.c */
  33. extern gx_device *gx_device_list[];
  34.  
  35. /* Device definitions */
  36. /* Following defines the null device */
  37. private dev_proc_fill_rectangle(null_fill_rectangle);
  38. private dev_proc_copy_mono(null_copy_mono);
  39. private dev_proc_get_xfont_procs(null_get_xfont_procs);
  40. private dev_proc_get_xfont_device(null_get_xfont_device);
  41.  
  42. /* The null device procedure record is also used to fill in */
  43. /* NULL procedures in actual devices, so it must be complete. */
  44. private gx_device_procs null_procs = {
  45.     gx_default_open_device,
  46.     gx_default_get_initial_matrix,
  47.     gx_default_sync_output,
  48.     gx_default_output_page,
  49.     gx_default_close_device,
  50.     gx_default_map_rgb_color,
  51.     gx_default_map_color_rgb,
  52.     null_fill_rectangle,
  53.     gx_default_tile_rectangle,
  54.     null_copy_mono,
  55.     gx_default_copy_color,
  56.     gx_default_draw_line,
  57.     gx_default_get_bits,
  58.     gx_default_get_props,
  59.     gx_default_put_props,
  60.     gx_default_map_cmyk_color,
  61.     null_get_xfont_procs,
  62.     null_get_xfont_device
  63. };
  64. gx_device_null gs_null_device = {
  65.     sizeof(gx_device),
  66.     &null_procs,
  67.     "null",
  68.     0, 0,
  69.     72, 72,
  70.     no_margins,
  71.     dci_black_and_white,
  72.     1,
  73.     0                /* target */
  74. };
  75.  
  76. /* Fill in a single procedure */
  77. #define fill_default(procs, p, dproc)\
  78.   if ( (procs)->p == 0 ) (procs)->p = dproc
  79. #define fill_proc(procs, p)\
  80.   fill_default(procs, p, null_procs.p)
  81.  
  82. /* Fill in NULL procedures in a device procedure record. */
  83. void
  84. gx_device_procs_complete(register gx_device_procs *procs)
  85. {    fill_proc(procs, open_device);
  86.     fill_proc(procs, get_initial_matrix);
  87.     fill_proc(procs, sync_output);
  88.     fill_proc(procs, output_page);
  89.     fill_proc(procs, close_device);
  90.     fill_proc(procs, map_rgb_color);
  91.     fill_proc(procs, map_color_rgb);
  92.     /* NOT fill_rectangle */
  93.     fill_proc(procs, tile_rectangle);
  94.     /* NOT copy_mono */
  95.     fill_proc(procs, copy_color);        /* Bogus? */
  96.     fill_proc(procs, draw_line);
  97.     fill_proc(procs, get_bits);
  98.     fill_proc(procs, get_props);
  99.     fill_proc(procs, put_props);
  100.     fill_proc(procs, map_cmyk_color);
  101.     fill_default(procs, get_xfont_procs, gx_default_get_xfont_procs);
  102.     fill_default(procs, get_xfont_device, gx_default_get_xfont_device);
  103. }
  104. void
  105. gx_device_complete_procs(gx_device *dev)
  106. {    gx_device_procs_complete(dev->procs);
  107. }
  108.  
  109. /* Flush buffered output to the device */
  110. int
  111. gs_flushpage(gs_state *pgs)
  112. {    gx_device *dev = pgs->device->info;
  113.     return (*dev->procs->sync_output)(dev);
  114. }
  115.  
  116. /* Make the device output the accumulated page description */
  117. int
  118. gs_copypage(gs_state *pgs)
  119. {    return gs_output_page(pgs, 1, 0);
  120. }
  121. int
  122. gs_output_page(gs_state *pgs, int num_copies, int flush)
  123. {    gx_device *dev = pgs->device->info;
  124.     return (*dev->procs->output_page)(dev, num_copies, flush);
  125. }
  126.  
  127. /* Copy scan lines from an image device */
  128. int
  129. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  130.   int *plines_copied, uint *pbytes_copied)
  131. {    uint line_size = gx_device_raster(dev, 0);
  132.     uint count = size / line_size;
  133.     uint i;
  134.     byte *dest = data;
  135.     for ( i = 0; i < count; i++, dest += line_size )
  136.     {    int code = (*dev->procs->get_bits)(dev, start_y + i, dest, NULL);
  137.         if ( code < 0 )
  138.         {    /* Might just be an overrun. */
  139.             if ( start_y + i == dev->height ) break;
  140.             return_error(code);
  141.         }
  142.     }
  143.     if ( plines_copied != NULL )
  144.       *plines_copied = i;
  145.     if ( pbytes_copied != NULL )
  146.       *pbytes_copied = i * line_size;
  147.     return 0;
  148. }
  149.  
  150. /* Get the current device from the graphics state */
  151. gx_device *
  152. gs_currentdevice(const gs_state *pgs)
  153. {    return pgs->device->info;
  154. }
  155.  
  156. /* Get the name of a device */
  157. const char *
  158. gs_devicename(const gx_device *dev)
  159. {    return dev->dname;
  160. }
  161.  
  162. /* Get the initial matrix of a device. */
  163. void
  164. gs_deviceinitialmatrix(gx_device *dev, gs_matrix *pmat)
  165. {    fill_proc(dev->procs, get_initial_matrix);
  166.     (*dev->procs->get_initial_matrix)(dev, pmat);
  167. }
  168.  
  169. /* Get the N'th device from the known device list */
  170. gx_device *
  171. gs_getdevice(int index)
  172. {    int i;
  173.     for ( i = 0; gx_device_list[i] != 0; i++ )
  174.        {    if ( i == index ) return gx_device_list[i];
  175.        }
  176.     return 0;            /* index out of range */
  177. }
  178.  
  179. /* Clone an existing device. */
  180. int
  181. gs_copydevice(gx_device **pnew_dev, const gx_device *dev, const gs_memory_procs *mprocs)
  182. {    register gx_device *new_dev;
  183.     new_dev = (gx_device *)(*mprocs->alloc)(1, dev->params_size, "gs_copydevice");
  184.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  185.     memcpy(new_dev, dev, dev->params_size);
  186.     new_dev->is_open = 0;
  187.     *pnew_dev = new_dev;
  188.     return 0;
  189. }
  190.  
  191. /* Make a memory (image) device. */
  192. /* If num_colors = -16, -24, or -32, this is a true-color device; */
  193. /* otherwise, num_colors is the number of elements in the palette */
  194. /* (2^N or 3*2^N). */
  195. int
  196. gs_makeimagedevice(gx_device **pnew_dev, const gs_matrix *pmat,
  197.   uint width, uint height, const byte *colors, int num_colors,
  198.   const gs_memory_procs *mprocs)
  199. {    const gx_device_memory *old_dev;
  200.     register gx_device_memory *new_dev;
  201.     int palette_size = num_colors;
  202.     int bpp = 1;
  203.     int pcount;
  204.     int bits_per_pixel;
  205.     float x_pixels_per_unit, y_pixels_per_unit;
  206.     byte palette[256 * 3];
  207.     byte *dev_palette;
  208.     int has_color;
  209.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  210.     switch ( num_colors )
  211.        {
  212.     case 3*2:
  213.         palette_size = 2; bpp = 3;
  214.     case 2:
  215.         bits_per_pixel = 1; break;
  216.     case 3*4:
  217.         palette_size = 4; bpp = 3;
  218.     case 4:
  219.         bits_per_pixel = 2; break;
  220.     case 3*16:
  221.         palette_size = 16; bpp = 3;
  222.     case 16:
  223.         bits_per_pixel = 4; break;
  224.     case 3*256:
  225.         palette_size = 256; bpp = 3;
  226.     case 256:
  227.         bits_per_pixel = 8; break;
  228.     case -16:
  229.         bits_per_pixel = 16; palette_size = 0; break;
  230.     case -24:
  231.         bits_per_pixel = 24; palette_size = 0; break;
  232.     case -32:
  233.         bits_per_pixel = 32; palette_size = 0; break;
  234.     default:
  235.         return_error(gs_error_rangecheck);
  236.        }
  237.     old_dev = gdev_mem_device_for_bits(bits_per_pixel);
  238.     if ( old_dev == 0 )        /* no suitable device */
  239.         return_error(gs_error_rangecheck);
  240.     pcount = palette_size * 3;
  241.     /* Check to make sure the palette contains white and black, */
  242.     /* and, if it has any colors, the six primaries. */
  243.     if ( bits_per_pixel <= 8 )
  244.        {    const byte *p;
  245.         byte *q;
  246.         int primary_mask = 0;
  247.         int i;
  248.         has_color = 0;
  249.         for ( i = 0, p = colors, q = palette;
  250.               i < palette_size; i++, q += 3
  251.             )
  252.            {    int mask = 1;
  253.             switch ( bpp )
  254.                {
  255.             case 1:            /* gray */
  256.                 q[0] = q[1] = q[2] = *p++;
  257.                 break;
  258.             default:        /* bpp == 3, colored */
  259.                 q[0] = p[0], q[1] = p[1], q[2] = p[2];
  260.                 p += 3;
  261.                }
  262. #define shift_mask(b,n)\
  263.   switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
  264.             shift_mask(q[0], 4);
  265.             shift_mask(q[1], 2);
  266.             shift_mask(q[2], 1);
  267. #undef shift_mask
  268.             primary_mask |= mask;
  269.             if ( q[0] != q[1] || q[0] != q[2] )
  270.                 has_color = 1;
  271.            }
  272.         switch ( primary_mask )
  273.            {
  274.         case 129:        /* just black and white */
  275.             if ( has_color )    /* color but no primaries */
  276.                 return_error(gs_error_ran